@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    @Transactional  // 声明式事务
    public void createUser(User user) {
        userRepository.save(user);
        // 模拟异常，测试事务回滚
        if (user.getName().contains("test")) {
            throw new RuntimeException("Test rollback");
        }
    }
}